package com.lefu.es.system;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wellness.scale.R;

/**
 * Created by zhanggonglin on 2017/2/18.
 */

public abstract class BaseActivity extends Activity {
    public static final String SYSTEM_EXIT = "com.afanda.driver.system_exit";
    private TextView mTvTitle;
    public ImageView mbtnBack;
    public TextView tv_back_title;
    public Context mContext;
    //private ExitReceiver mExitReceiver;
    //private NotNetworkRemindReceiver mNetworkRemin;
    public TextView mTvNotWifi;
    private NotNetworkRemindReceiver mNetworkRemin;
    // private PromptDialog mdialog ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        initView();
        findViews();
        init();
        setListeners();
    }


    /**
     * 获取页面布局ID
     *
     * @return ID
     */
    protected abstract int getLayoutId();

    /**
     * 绑定控件
     */
    protected abstract void findViews();

    /**
     * 初始化操作
     */
    protected abstract void init();

    /**
     * 设置监听
     */
    protected abstract void setListeners();
    private ExitReceiver mExitReceiver;

    private void initView() {
        mContext = this;
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        try {
            tv_back_title = (TextView) findViewById(R.id.tv_back_title);
            tv_back_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            mbtnBack = (ImageView) findViewById(R.id.iv_back);
            mbtnBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } catch (Exception e) {
        }
      /*  try {
            mTvNotWifi = (TextView) findViewById(R.id.tv_not_wifi);
        } catch (Exception e) {

        }*/

        //注册广播 检测网络是否正常
       IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        mNetworkRemin = new NotNetworkRemindReceiver();
        /*this.registerReceiver(mNetworkRemin, intentFilter);

        IntentFilter filter = new IntentFilter();
        filter.addAction(SYSTEM_EXIT);
        mExitReceiver = new ExitReceiver();
        this.registerReceiver(mExitReceiver, filter);*/


    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    class ExitReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }

    class NotNetworkRemindReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                checkNetWorkStatus();
            }
        }

        private void checkNetWorkStatus() {
            ConnectivityManager mConnectivityManager = (ConnectivityManager)
                    mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo == null) {//无网络
                if (null == mTvNotWifi)
                    return;
                mTvNotWifi.setVisibility(View.VISIBLE);
                mTvNotWifi.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Settings.ACTION_SETTINGS);
                        startActivityForResult( intent , 0);
                    }
                });
            }
            else {//有网络
                if (null != mTvNotWifi)
                    mTvNotWifi.setVisibility(View.GONE);
            }
        }

    }
}
